package com.cypher.encryptionanddecryption.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.cypher.encryptionanddecryption.R;
import com.cypher.encryptionanddecryption.activity.MainActivity;
import com.cypher.encryptionanddecryption.bluetooth.BluetoothManagerSingleton;
import com.cypher.encryptionanddecryption.bluetooth.ExpandBroadcastReceiver;

/**
 * 通信列表悬浮刷新按钮
 * @author: 古霸天
 * @date: 2020/11/4
 */
public class CommunicationFloatingFragment extends Fragment {
    public Button flushBtn;
    private Activity mainActivity;
    private CommunicationFragment communicationFragment;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_communication_floating, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {

        flushBtn = view.findViewById(R.id.flush_communicate_btn);

        mainActivity = BluetoothManagerSingleton.getInstance().getActivity();
        communicationFragment = ((MainActivity)mainActivity).getCommunicationFragment();
        // 按钮设置监听事件，开启蓝牙扫描
        View.OnClickListener flushBtnClick = (v) -> {

            // 如果网络未连接，直接返回操作
            if (!ExpandBroadcastReceiver.network) {
                Toast.makeText(mainActivity, "网络未连接或不可使用", Toast.LENGTH_SHORT).show();
                return;
            }
            Integer protocolId = ((MainActivity) mainActivity).getProtocolId();
            if (protocolId == null) {
                Toast.makeText(mainActivity, "请先在设置界面设置协议类型", Toast.LENGTH_SHORT).show();
                return;
            }
            // 清空列表
            communicationFragment.clearList();
            // 渲染列表
            communicationFragment.readerListView(
                    communicationFragment.getSearchTxt().getText().toString().trim());

        };
        flushBtn.setOnClickListener(flushBtnClick);
    }

    /**
     * 设置按钮动画效果
     */
    public void setBtnAnimation() {
        final RotateAnimation animation = new RotateAnimation(0.0f, 360.0f,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        animation.setDuration(1000);
        animation.setRepeatCount(-1);
        animation.setInterpolator(new LinearInterpolator());
        if (flushBtn != null) {
            flushBtn.startAnimation(animation);
        }
    }

    /**
     * 清除按钮动画效果
     */
    public void clearBtnAnimation() {
        flushBtn.clearAnimation();
    }
}
